//+------------------------------------------------------------------+
//| 测试MA填充效果                                                    |
//+------------------------------------------------------------------+
#property copyright "Test"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   3

// MA参数
input int MA5Period = 5;
input int EMA144Period = 144;

//--- plot MA5线
#property indicator_label1  "MA5"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- plot EMA144线
#property indicator_label2  "EMA144"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrWhite
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- plot 填充区域
#property indicator_label3  "填充区域"
#property indicator_type3   DRAW_FILLING
#property indicator_color3  clrLightGreen,clrLightCoral
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1

//--- 缓冲区
double MA5Buffer[];
double EMA144Buffer[];
double FillBuffer1[];
double FillBuffer2[];

//--- 句柄
int ma5_handle;
int ema144_handle;

//+------------------------------------------------------------------+
//| 初始化函数                                                        |
//+------------------------------------------------------------------+
int OnInit()
{
   // 缓冲区映射
   SetIndexBuffer(0, MA5Buffer, INDICATOR_DATA);
   SetIndexBuffer(1, EMA144Buffer, INDICATOR_DATA);
   SetIndexBuffer(2, FillBuffer1, INDICATOR_DATA);
   SetIndexBuffer(3, FillBuffer2, INDICATOR_DATA);
   
   // 设置空值
   PlotIndexSetDouble(2, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   
   // 创建MA句柄
   ma5_handle = iMA(NULL, 0, MA5Period, 0, MODE_SMA, PRICE_CLOSE);
   ema144_handle = iMA(NULL, 0, EMA144Period, 0, MODE_EMA, PRICE_CLOSE);
   
   if(ma5_handle == INVALID_HANDLE || ema144_handle == INVALID_HANDLE)
   {
      Print("Failed to create MA handles");
      return(INIT_FAILED);
   }
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| 计算函数                                                          |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   if(rates_total < EMA144Period) return(0);
   
   // 获取MA数据
   double ma5_data[], ema144_data[];
   ArrayResize(ma5_data, rates_total);
   ArrayResize(ema144_data, rates_total);
   
   if(CopyBuffer(ma5_handle, 0, 0, rates_total, ma5_data) <= 0 ||
      CopyBuffer(ema144_handle, 0, 0, rates_total, ema144_data) <= 0)
   {
      return(0);
   }
   
   int limit = prev_calculated == 0 ? EMA144Period : prev_calculated - 1;
   
   for(int i = limit; i < rates_total; i++)
   {
      // 设置MA值
      MA5Buffer[i] = ma5_data[i];
      EMA144Buffer[i] = ema144_data[i];
      
      // 设置填充区域
      FillBuffer1[i] = MA5Buffer[i];
      FillBuffer2[i] = EMA144Buffer[i];
   }
   
   return(rates_total);
}

//+------------------------------------------------------------------+
//| 去初始化函数                                                      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   if(ma5_handle != INVALID_HANDLE)
      IndicatorRelease(ma5_handle);
   if(ema144_handle != INVALID_HANDLE)
      IndicatorRelease(ema144_handle);
}